using System;
using System.Collections.Generic;
using System.Text;

namespace Autorun
{
    public class DayRange
    {
        #region "Members"
        private const string VALID_SIGNS = "01234567890-,";
        private bool[] _data = new bool[33];
        #endregion

        #region "Constructors"
        public DayRange()
        {
        }

        public DayRange(int rawValue)
        {
            for (int i = 0; i < 32; i++)
                if ((rawValue & (int)Math.Pow(2, i)) != 0)
                    _data[i + 1] = true;
        }
        #endregion

        #region "Properties"
        public bool[] Value
        {
            get
            {
                return _data;
            }
        }

        public int RawValue
        {
            get
            {
                int rc;

                rc = 0;
                for (int i = 1; i < 33; i++)
                    if (_data[i] == true)
                        rc = rc | (int)Math.Pow(2, (i - 1));
                return rc;
            }
        }
        #endregion

        #region "Methods"
        public static bool TryParse(string s, out DayRange result)
        {
            string[] items;
            string[] subItems;
            int idxFrom;
            int idxTo;
            DayRange rc;

            result = null;
            rc = new DayRange();

            if ((s == null) || (s.Length == 0))
                return false;
            else
            {
                s = s.Replace(" ", "");
                foreach (char c in s)
                    if (VALID_SIGNS.Contains(c.ToString()) == false)
                        return false;

                items = s.Split(',');
                foreach (string item in items)
                {
                    subItems = item.Split('-');
                    if (subItems.Length > 2)
                        return false;

                    idxFrom = int.Parse(subItems[0]);
                    idxTo = int.Parse(subItems.Length == 1 ? subItems[0] : subItems[1]);
                    if ((idxFrom > idxTo) || (idxFrom > 32) || (idxTo > 32) || (idxFrom < 1) || (idxTo < 1))
                        return false;
                    for (int i = idxFrom; i <= idxTo; i++)
                        rc._data[i] = true;
                }
            }

            result = rc;
            return true;
        }

        public override string ToString()
        {
            string rc;
            int idxFrom;
            int idxTo;

            rc = null;

            idxFrom = 0;
            idxTo = 0;

            if (RawValue == 0)
                return null;

            for (int i = 1; i < 33; i++)
                if (_data[i] == true)
                    if (idxFrom == 0)
                    {
                        idxFrom = i;
                        idxTo = i;
                    }
                    else
                        idxTo = i;
                else
                    if (idxFrom != 0)
                    {
                        if (idxFrom == idxTo)
                            rc += idxFrom + ",";
                        else
                            rc += idxFrom + "-" + idxTo + ",";
                        idxFrom = 0;
                        idxTo = 0;
                    }
            if (idxFrom == idxTo)
                rc += idxFrom + ",";
            else
                rc += idxFrom + "-" + idxTo + ",";

            rc = rc.Remove(rc.Length - 1); // remove the last comma
            return rc;
        }
        #endregion
    }
}
